package com.example.mqttmanage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mqttmanage.domain.base.ResponseResult;
import com.example.mqttmanage.domain.device.Device;
import com.example.mqttmanage.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private IDeviceService deviceService;

    // 分页查询
    @GetMapping("/list")
    public ResponseResult<Page<Device>> list(@RequestParam(defaultValue = "1") Long pageNum,
                                             @RequestParam(defaultValue = "10") Long pageSize,
                                             Device device) {
        return deviceService.selectPageDevice(pageNum, pageSize, device);
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public ResponseResult<Device> getInfo(@PathVariable Long id) {
        return deviceService.selectById(id);
    }

    // 新增设备
    @PostMapping
    public ResponseResult<Device> add(@RequestBody Device device) {
        return deviceService.insertDevice(device);
    }

    // 修改设备
    @PutMapping
    public ResponseResult<Device> edit(@RequestBody Device device) {
        return deviceService.updateDevice(device);
    }

    // 删除设备
    @DeleteMapping("/{id}")
    public ResponseResult<String> remove(@PathVariable Long id) {
        return deviceService.deleteById(id);
    }

    // 批量删除设备
    @DeleteMapping("/batch/{ids}")
    public ResponseResult<String> removeBatch(@PathVariable List<Long> ids) {
        return deviceService.deleteBatch(ids);
    }
}